<?php
/**
 * 商品列表页面
 * Created by Sublime Text 3.
 * @authors Rejeo (rejeo@qq.com)
 * @date    2018-05-07 14:51:10
 * @version $Id$
 */
namespace app\index\controller;

use app\index\controller\Base;
use think\Request;


class Lists extends Base
{
    public function index(Request $request)
    {
        $data             = [];
        $orders           = [];
        $firstCategoryId  = [];
        $sedCategorys     = [];
        $categoryParentId = 0;
        $id               = $request->get('id', 0, 'intval');
        $order_sales      = $request->get('order_sales', '');
        $order_price      = $request->get('order_price', '');
        $order_time       = $request->get('order_time', '');
        $categorys = model('category')->getNormalFirstCategory();
        foreach ($categorys as $value) {
            $firstCategoryId[] = $value->id;
        }
        // id 属于一级分类
        if (in_array($id, $firstCategoryId)) {
            $categoryParentId = $id;
            $data['category_id'] = $id;
        }elseif ($id) { // 二级分类
            // 获取二级分类数据
            $category = model('category')->get($id);
            if (!$category || 1 !== $category->status) {
               $this->error('数据不合法！');
            }
            $categoryParentId = $category->parent_id;
            $data['se_category_id'] = $category->parent_id;
        }else{
            $categoryParentId = 0;
        }

        // 获取子栏目
        if ($categoryParentId) {
            $sedCategorys = model('category')->getCategorysToParentId($categoryParentId);
        }

        // 排序
        if (!empty($order_sales)) {
            $orderflag = 'order_sales';
            $order['order_sales'] = $order_sales;
        } elseif (!empty($order_price)) {
            $orderflag = 'order_price';
            $order['order_price'] = $order_price;
        }elseif (!empty($order_time)) {
            $orderflag = 'order_time';
            $order['order_time'] = $order_time;
        }else{
            $orderflag = '';
        }
        $data['city_id'] = $this->city->id;// 值来至于继承base控制器
        // 根据条件，对商品进行排序
        // $deals = model('Deal')->getDealByConditions($data, $orders);
        // $page = $deals->appends($request->param())->render();
        $this->assign([
            'categorys'        => $categorys,
            'sedCategorys'     => $sedCategorys,
            'id'               => $id,
            'categoryParentId' => $categoryParentId,
            'orderflag'        => $orderflag,
            // 'deals'            => $deals,
            // 'page'             => $page,
        ]);
        return $this->fetch();
    }
}
